import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
import vuexPersist from "vuex-persist";

export default new Vuex.Store({
  state: {
    list: [
      { id: 1, name: "TCL彩电", price: 1000, num: 1 },
      { id: 2, name: "机顶盒", price: 2000, num: 2 },
      { id: 3, name: "鸿星尔克", price: 50000000, num: 5 },
      { id: 4, name: "辽宁正大", price: 200000000, num: 1 },
    ],
  },
  mutations: {
    remove(state, index) {
      state.list.splice(index, 1);
    },
    change() {},
  },
  actions: {},
  modules: {},
  getters: {
    sum(state) {
      let num = 0;
      state.list.forEach((item) => {
        num += item.num * item.price;
      });
      return num;
    },
  },
  plugins: [
    new vuexPersist({
      storage: window.localStorage,
    }).plugin,
  ],
  // 插件
});
